package com.taotao.rest.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.pojo.ItemCatNode;
import com.taotao.rest.service.ItemCatService;

@Service
public class ItemCatServiceImpl implements ItemCatService{
	
	@Autowired
	private TbItemCatMapper itemCatMapper;
	@Autowired
	private JedisClient jedisClient;
	@Value("${TB_ITEM_CAT_PARENT_KEY}")
	private String TB_ITEM_CAT_PARENT_KEY;
	
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map getItemCatList(){
		Map map=new HashMap();
		
		List<TbItemCat> roots=getContentCatByParentId(0);
		List<ItemCatNode> list=new ArrayList<ItemCatNode>();
		for(TbItemCat root : roots) {
			//设置根节点
			ItemCatNode itemCatNode = new ItemCatNode();
			
			list.add(itemCatNode);
			
			itemCatNode.setName("<a href='/products/1.html'>"+root.getName()+"</a>");
			itemCatNode.setUrl("/products/"+root.getId()+".html");
			
			//获取第二层的item
			List<TbItemCat> snodes=getContentCatByParentId(root.getId());
			
			List<ItemCatNode> second = new ArrayList<ItemCatNode>();
			
			itemCatNode.setItem(second);
			
			for(TbItemCat snode : snodes) {
				//设置第二层的节点
				ItemCatNode secondNode = new ItemCatNode();
				second.add(secondNode);
				secondNode.setName(snode.getName());
				secondNode.setUrl("/products/"+snode.getId()+".html");
				
				
				//获取第三层的item
				List<TbItemCat> tnodes=getContentCatByParentId(snode.getId());
				
				List<String> item=new ArrayList<String>();
				secondNode.setItem(item);
				for(TbItemCat tnode : tnodes) {
					item.add("/products/"+tnode.getId()+".html|"+tnode.getName());
				}
				
			}
			
		}
		map.put("data", list);
		
		return map;
	}
	private List<TbItemCat> getContentCatByParentId(long pid){
		//获取Redis缓存
		String key=pid+"";
		try {
			String hget = jedisClient.hget(TB_ITEM_CAT_PARENT_KEY, key);
			if(!StringUtils.isBlank(hget)) {
				List<TbItemCat> list = JsonUtils.jsonToList(hget, TbItemCat.class);
				return list;
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		
		TbItemCatExample example=new TbItemCatExample();
		example.createCriteria().andParentIdEqualTo(pid);
		List<TbItemCat> list = itemCatMapper.selectByExample(example);
		
		//设置Redis缓存
		try {
			if(!list.isEmpty()) {
				String data=JsonUtils.objectToJson(list);
				jedisClient.hset(TB_ITEM_CAT_PARENT_KEY, key, data);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
}
